package com.application.mestory;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! the client locale is "+ locale.toString());
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		model.addAttribute("serverTime", formattedDate );
		
		return "home";
	}
	
	@RequestMapping(value = "/client/adminMyStory", method = RequestMethod.GET)
	public String adminMyStory(Locale locale, Model model) {
		return "client/adminMyStory";
	}
	
	@RequestMapping(value = "/story", method = RequestMethod.GET)
	public String story(Locale locale, Model model) {
		logger.info("upload item");
		
		
		model.addAttribute("mysStory", "Long, long time ago...");
		
		return "story";
	}
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	@ResponseBody
	public String handleFormUpload(@RequestParam("uploadFile") MultipartFile uploadFile, HttpServletRequest request) {
	 
	    // Some type of file processing...
	   logger.info("-------------------------------------------");
	   logger.info("upload: " + uploadFile.getOriginalFilename());
	   logger.info("upload: " + uploadFile.getContentType());
	   logger.info("-------------------------------------------");
	    
	    
	    if (!uploadFile.isEmpty()) {
            File file = new File(request.getSession().getServletContext().getRealPath("/resources/images") + File.separator + uploadFile.getOriginalFilename());
			try {
				FileUtils.writeByteArrayToFile(file, uploadFile.getBytes());
				logger.info(request.getSession().getServletContext().getRealPath("/resources/images") + File.separator + uploadFile.getOriginalFilename() + " successfully uploaded.");
				logger.info("-------------------------------------------");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	    }
		return "{success:true}";
	}
}
